#include "gudb.h"
#include "wal_manager.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main() {
    printf("=== WAL Manager Test ===\n");
    
    // Create a temporary directory for testing
    system("mkdir -p /tmp/gudb_wal_test");
    
    // Test 1: WAL Manager creation
    printf("1. Testing WAL manager creation...\n");
    wal_manager_t* wal = wal_manager_create("/tmp/gudb_wal_test");
    if (!wal) {
        printf("❌ Failed to create WAL manager\n");
        return 1;
    }
    printf("✅ WAL manager created successfully\n");
    
    // Test 2: WAL Manager start (simplified without threads)
    printf("2. Testing WAL manager configuration...\n");
    wal_set_sync_interval(wal, 100);  // Fast sync for testing
    wal_set_checkpoint_interval(wal, 1);  // Fast checkpoint for testing
    printf("✅ WAL manager configured successfully\n");
    
    // Test 3: Record insertion
    printf("3. Testing WAL record insertion...\n");
    const char* test_data = "test_insert_data";
    lsn_t lsn = wal_insert_record(wal, WAL_INSERT, 1, test_data, strlen(test_data));
    if (lsn == INVALID_LSN) {
        printf("❌ Failed to insert WAL record\n");
    } else {
        printf("✅ WAL record inserted with LSN: %lu\n", lsn);
    }
    
    // Test 4: Transaction logging
    printf("4. Testing transaction logging...\n");
    lsn_t begin_lsn = wal_log_transaction_begin(wal, 1, ISOLATION_READ_COMMITTED);
    lsn_t commit_lsn = wal_log_transaction_commit(wal, 1);
    if (begin_lsn != INVALID_LSN && commit_lsn != INVALID_LSN) {
        printf("✅ Transaction logged successfully (begin: %lu, commit: %lu)\n", begin_lsn, commit_lsn);
    } else {
        printf("❌ Failed to log transaction\n");
    }
    
    // Test 5: WAL statistics
    printf("5. Testing WAL statistics...\n");
    wal_print_stats(wal);
    
    // Test 6: WAL manager cleanup
    printf("6. Testing WAL manager cleanup...\n");
    wal_manager_destroy(wal);
    printf("✅ WAL manager destroyed successfully\n");
    
    printf("\n=== WAL Test Complete ===\n");
    
    // Cleanup test directory
    system("rm -rf /tmp/gudb_wal_test");
    
    return 0;
}